package com.xianbeiying.app.ckgl.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.lidroid.xutils.util.LogUtils;
import com.xianbeiying.app.ckgl.MyApplication;
import com.xianbeiying.app.ckgl.R;
import com.xianbeiying.app.ckgl.adapter.RbFragmentAdapter;
import com.xianbeiying.app.ckgl.bean.DevicesBean;
import com.xianbeiying.app.ckgl.bean.FaMenBean;
import com.xianbeiying.app.ckgl.bean.FaMenTypeBean;
import com.xianbeiying.app.ckgl.bean.JsonBean;
import com.xianbeiying.app.ckgl.bean.JsonDataBean;
import com.xianbeiying.app.ckgl.bean.OpenFmBean;
import com.xianbeiying.app.ckgl.bean.SFmTypeBean;
import com.xianbeiying.app.ckgl.bean.UrlBean;
import com.xianbeiying.app.ckgl.data.Cmd;
import com.xianbeiying.app.ckgl.data.DataUtil;
import com.xianbeiying.app.ckgl.data.EQP;
import com.xianbeiying.app.ckgl.http.HttpCallBackUtil;
import com.xianbeiying.app.ckgl.model.GetModel;
import com.xianbeiying.app.ckgl.model.SetModel;
import com.xianbeiying.app.ckgl.util.StringUtil;
import com.xianbeiying.app.ckgl.util.ToastUtil;
import com.xianbeiying.app.ckgl.widght.dapengView.DpView;
import com.xianbeiying.app.ckgl.widght.dapengView.Dpdata;

import java.util.ArrayList;
import java.util.List;

/**
 * 卷帘
 */
public class RbFragment extends BaseFragment  implements HttpCallBackUtil,Dpdata{
   private View view;
    //水平滑动的
    private LinearLayout ly_rb_horizontal;
    private View va_line;
    private  TextView tv_va_all;
    private ListView rb_list;
    private static RbFragment rbFragment;
    public static RbFragment getInstance(){
        rbFragment=new RbFragment();
        return rbFragment;
    }
    private SetModel setModel;
    private GetModel getModel;
    private RbFragmentAdapter rbFragmentAdapter;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view=inflater.inflate(R.layout.fragment_rb,null);
        initView();
        return view;
    }
    private void initView() {
        va_line=(View) view.findViewById(R.id.va_line);
        tv_va_all=(TextView) view.findViewById(R.id.tv_va_all);
        ly_rb_horizontal=(LinearLayout) view.findViewById(R.id.ly_rb_horizontal);
        rb_list=(ListView) view.findViewById(R.id.rb_list);
//            getData(DataUtil.dapeng.length,0);
        tv_va_all.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v){
                va_line.setVisibility(View.GONE);
                tv_va_all.setVisibility(View.GONE);
            }
        });
        setModel=new SetModel(getActivity());
        getModel=new GetModel(getActivity());
        setModel.addResponseListener(this);
        getModel.addResponseListener(this);
        initData();

    }
    private int faId;
    private void initData() {
        if(MyApplication.devicesBean!=null){
            LogUtils.e("---------进入路");
            devicesBean=MyApplication.devicesBean;
            getData(devicesBean.getGREENHOUSE(),devicesBean.getGREENHOUSE().size(), 0);
            getModel.QUWEYDEVSDetauls("GET_ALLDEVICES_STATUS", devicesBean.getGREENHOUSE().get(faId).getGreenhouseId(), false);
        }else{
            getModel.QUWEYDEVS(true);
        }
    }
    private DpView dpView;
    private void getData(List<DevicesBean.GREENHOUSEBean> DEVICES, int size,final int id){
        dpView = new DpView(DEVICES, size,id, ly_rb_horizontal, getActivity());
        dpView.setDpdata();
        dpView.addResponseListener(this);
    }
    private DevicesBean devicesBean;
    private JsonDataBean jsonDataBean;
    private FaMenBean faMenBean;
    private ArrayList<FaMenBean.VBean.STATUSBean> listFa;
    String[] conts;
    private ArrayList<Boolean> arrayChoiceList;
    private FaMenTypeBean faMenTypeBean;
    private String[] fmType;
    private OpenFmBean openFmBean;

    @Override
    public void onCallBackData(String url, Object content) {
        if(url.equals(UrlBean.QUWEYDEVS)){
            if(content!=null){
                devicesBean= JSON.parseObject(new String((byte[])content),DevicesBean.class);
                MyApplication.devicesBean=devicesBean;
                getData(devicesBean.getGREENHOUSE(),devicesBean.getGREENHOUSE().size(), 0);
                getModel.QUWEYDEVSDetauls("GET_ALLDEVICES_STATUS", devicesBean.getGREENHOUSE().get(faId).getGreenhouseId(), false);
            }}
        if (url.equals("大棚信息")) {
            if (content != null) {
                faMenBean = JSON.parseObject(new String((byte[]) content), FaMenBean.class);
                listFa = DataUtil.getSeBei(faMenBean, EQP.EQP_TYPE_RSM);
//                listFa = DataUtil.fanmens(faMenBean);
                conts = DataUtil.getFamen(listFa);
                arrayChoiceList = StringUtil.getList(conts.length);
//                jsonDataBean = JsonBean.setSFmData(devicesBean, listFa, faId);
                fmType = DataUtil.getFmOcc(listFa);
                rbFragmentAdapter=new RbFragmentAdapter(getActivity(),listFa);
                rb_list.setAdapter(rbFragmentAdapter);
                rbFragmentAdapter.setHandler(handler);
//                //获取卷帘信息
//                getModel.QUWEYJLDetauls(jsonDataBean, false);
            }
        }
//        if (url.equals("智能卷帘")){
//            if (content != null) {
//                faMenTypeBean = JSON.parseObject(new String((byte[]) content), FaMenTypeBean.class);
//                if (faMenBean.isR()) {
//                    fmType = DataUtil.getFmOc(faMenTypeBean);
////                    rbFragmentAdapter=new RbFragmentAdapter(getActivity(),faMenTypeBean.getV().getEQPS());
////                    rb_list.setAdapter(rbFragmentAdapter);
////                    rbFragmentAdapter.setHandler(handler);
//                } else {
//                    ToastUtil.showInfo(getActivity(), "访问失败");
//                }
//            }
//        }
        if (url.equals("设置卷帘")) {
            if (content != null) {
                openFmBean = JSON.parseObject(new String((byte[]) content), OpenFmBean.class);
                if (openFmBean.isR()){
                    jsonDataBean = JsonBean.setSFmData(devicesBean, listFa, faId);
//                 getModel.QUWEYJLDetauls(jsonDataBean, false);
                    getModel.QUWEYDEVSDetauls("GET_ALLDEVICES_STATUS", devicesBean.getGREENHOUSE().get(faId).getGreenhouseId(), true);
                    ToastUtil.showInfo(getActivity(), "操作成功");
                } else {
                    ToastUtil.showInfo(getActivity(), openFmBean.getERR_MSG());
                }
            }
        }

    }
    SFmTypeBean sFmTypeBean;
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            int crid=listFa.get(msg.arg1).getCTRLID();
            int eqid=listFa.get(msg.arg1).getDEVID();
            int eqtype=listFa.get(msg.arg1).getDEVTYPE();
            switch (msg.what){
                //打开
                case R.id.rb_open:
                    sFmTypeBean = JsonBean.setJLTypeData(crid,eqid,eqtype, Cmd.EQP_ROLLING_STATUS_OPEN,devicesBean, listFa.get(msg.arg1));
                    setModel.SETJLDetauls(sFmTypeBean, true);
                    break;
                //关闭
                case  R.id.rb_close:
                    sFmTypeBean = JsonBean.setJLTypeData(crid,eqid,eqtype, Cmd.EQP_ROLLING_STATUS_CLOSE,devicesBean, listFa.get(msg.arg1));
                    setModel.SETJLDetauls(sFmTypeBean, true);
                    break;
                //停止
                case  R.id.rb_middle:
                    sFmTypeBean = JsonBean.setJLTypeData(crid,eqid,eqtype, Cmd.EQP_ROLLING_STATUS_STOP,devicesBean, listFa.get(msg.arg1));
                    setModel.SETJLDetauls(sFmTypeBean, true);
                    break;
            }
        }
    };

    @Override
    public void onDpdata(int sid, boolean isChoice){
        if (isChoice){
            faId = sid;
            getModel.QUWEYDEVSDetauls("GET_ALLDEVICES_STATUS", devicesBean.getGREENHOUSE().get(faId).getGreenhouseId(), true);
        }
    }
}
